﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeThanhDia.MapAndUpgrade
{
    class FileThanh
    {
        public static bool[] isOpens = new bool[60];
        public static int[] Stars = new int[60];
        public static int[] HighScore = new int[60];
        static string temp;
        static IsolatedStorageFile storage;
        public FileThanh()
        {
            getData();
        }
        public static void getData()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("bvtd_OpenMap.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_OpenMap.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0;1;0;0");
                    for (int i = 1; i < 60; i++)
                    {
                        writer.WriteLine(i.ToString() + ";0;0;0");
                    }
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("bvtd_OpenMap.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    bool t = false;
                    if (result[1].Equals("0")) t = false;
                    else t = true;
                    isOpens[Int16.Parse(result[0])] = t;
                    Stars[Int16.Parse(result[0])] = Int16.Parse(result[2]);
                    HighScore[Int16.Parse(result[0])] = Int16.Parse(result[3]);
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
        public static void SaveFile(int IdThanh, int Star, int Score)
        {
            isOpens[IdThanh] = true;
            Stars[IdThanh] = Star;
            HighScore[IdThanh] = Score;
            if (IdThanh != 59)
            {
                isOpens[IdThanh + 1] = true;
            }
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("bvtd_OpenMap.txt"))
            {
                storage.DeleteFile("bvtd_OpenMap.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("bvtd_OpenMap.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                for (int i = 0; i < 60; i++)
                {
                    writer.WriteLine(i + ";" + (isOpens[i] == true ? "1" : "0") + ";" + Stars[i].ToString() + ";" + HighScore[i].ToString());
                }
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
    }
}
